package com.nextgen.services;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.nextgen.bean.CustomerDetails;
import com.nextgen.mongo.core.MongoInstance;
  
/**  
 * The Class CustomerDetailsService.  
 */  
@Service  
public class CustomerDetailsService {  
  
     private static final Logger logger_c = Logger.getLogger(CustomerDetailsService.class);  
  
     /**  
      * Get the customerDetails by id.  
      *  
      * @param customerDetailsId_p  
      *      the customerDetails id_p  
      * @return the customerDetails by id  
      */  
	public CustomerDetails getCustomerDetailsById(String customerDetailsId_p) {
		MongoOperations mongoOperation = MongoInstance.getInstance();
		Query searchUserQuery = new Query(Criteria.where("userId").is(customerDetailsId_p));
		
		// find the saved user again.
		CustomerDetails customerDetails = mongoOperation.findOne(searchUserQuery, CustomerDetails.class);
		logger_c.debug("CustomerDetails details with id : " + customerDetailsId_p + " is : "  + customerDetails);
		return customerDetails;
	}  
  
     /**  
      * Gets all customerDetails.  
      *  
      * @return the all customerDetails  
      */  
     public List<CustomerDetails> getAllCustomerDetails() {
    	 MongoOperations mongoOperation = MongoInstance.getInstance();
    	 List<CustomerDetails> customerDetails = mongoOperation.findAll(CustomerDetails.class);
    	 
    	 return customerDetails;  
     }  
  
     /**  
      * Creates the customerDetails.  
      *  
      * @param customerDetails_p  
      *      the customerDetails_p  
      * @return the customerDetails  
      */  
     public CustomerDetails createCustomerDetails(CustomerDetails customerDetails) {  
  
          logger_c.debug("Persisting customerDetails in database: " + customerDetails.toString());  
          MongoOperations mongoOperation = MongoInstance.getInstance();
          mongoOperation.save(customerDetails);  
  
          return customerDetails;  
     }  
  
     /**  
      * Update customerDetails.  
      *  
      * @param customerDetails_p  
      *      the customerDetails_p  
      * @return the customerDetails  
      */  
     public CustomerDetails updateCustomerDetails(CustomerDetails customerDetails_p) {  
  
          logger_c.debug("Updating customerDetails in database: " + customerDetails_p.toString());  
          customerDetails_p.setLastUpdated(new Date());
          MongoOperations mongoOperation = MongoInstance.getInstance();
          Query searchUserQuery = new Query(Criteria.where("userId").is(customerDetails_p.getUserId()));
          mongoOperation.updateFirst(searchUserQuery, 
                  Update.update("password", "new password"),CustomerDetails.class);
  
          return customerDetails_p;  
     }  
  
     /**  
      * Delete customerDetails.  
      *  
      * @param customerDetailsId_p  
      *      the customerDetails id_p  
      */  
     public void deleteCustomerDetails(String customerDetailsId_p) {  
          logger_c.debug("Deleting customerDetails from database: " + customerDetailsId_p.toString());
          MongoOperations mongoOperation = MongoInstance.getInstance();
          Query searchUserQuery = new Query(Criteria.where("userId").is(customerDetailsId_p));
          mongoOperation.remove(searchUserQuery, CustomerDetails.class);
     }  
}